home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-2.iso / Files II / Prog / N-P / OOP for C.sit / OIC.ƒ / indexMixin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-09  |  2.4 KB  |  121 lines  |  [TEXT/KAHL]

  1. /*    
  2.  *        IndexMixin abstract class
  3.  *
  4.  *        a general class mixin which maintains a sequenceable list of
  5.  *        all the class's instances. 
  6.  *
  7.  *            Copyright © John Wainwright 1988
  8.  *
  9.  *    MetaClass :
  10.  *
  11.  *    SuperClasses :
  12.  *
  13.  *    Class Vars :
  14.  *    
  15.  *        instanceList    - head of the instance list
  16.  *
  17.  *    Methods :
  18.  *
  19.  *        new                - new demon to build the instance list
  20.  *
  21.  *    Class Methods :
  22.  *
  23.  *        sequence obj    - returns an sequence object for the instance list
  24.  *        allInstances    - returns a List of all the instances
  25.  *        deepInstances    - returns a List of all specialised instances
  26.  */
  27.  
  28. #include "oic.h"
  29. #include "generics.h"
  30.  
  31. class       IndexMixin;            /* the IndexMixin class                 */
  32.  
  33. struct indexMixin_c                /* IndexMixin class var structure         */
  34. {
  35.     object    imc_head;            /* head of the instance list             */
  36.     int        imc_count;            /* how many in list                        */
  37. };
  38. typedef struct indexMixin_c indexMixin_c;
  39.  
  40. struct indexMixin_i                /* IndexMixin instance structure         */
  41. {
  42.     object    im_next;
  43. };
  44. typedef struct indexMixin_i indexMixin_i;
  45.  
  46. /* -------------------- IndexMixin Instance methods ---------------------------- */
  47.  
  48. static object
  49. _new(self, im, argp)
  50.     object                    self;
  51.     register indexMixin_i    *im;
  52.     object                    *argp;
  53. {
  54.     register indexMixin_c    *imc;
  55.     
  56.     imc = localCVs(self, indexMixin_c);
  57.     im->im_next = imc->imc_head;
  58.     imc->imc_head = self;
  59.     imc->imc_count += 1;
  60.  
  61.     return self;
  62. }
  63.  
  64. /* -------------------- IndexMixin Class methods ---------------------------- */
  65.  
  66. static object
  67. _sequence(class, cv)
  68.     class              class;
  69.     indexMixin_c    *cv;
  70. {
  71.     return start(New(Linkseq), allInstances(class));
  72. }
  73.  
  74. static object
  75. _allInstances(class, cv)
  76.     class              class;
  77.     indexMixin_c    *cv;
  78. {
  79.     register object    list;
  80.     register object    c;
  81.  
  82.     list = New(List, END);
  83.     for (c = cv->imc_head; c != END; c = localIVs(c, indexMixin_i)->im_next)
  84.         push(list, c);
  85.         
  86.     return list;
  87. }
  88.  
  89. static object
  90. _deepInstances(class)
  91.     class              class;
  92. {
  93.     register object    list;
  94.     register object    seq;
  95.     register object    c;
  96.     
  97.     list = allInstances(class);
  98.     for (seq = sequence(subs(class)); c = next(seq); )
  99.         join(list, allInstances(c));
  100.     dispose(seq);
  101.     return list;
  102. }
  103.  
  104. /* ------------------- Init the IndexMixin class ------------------------------- */
  105.  
  106. _InitIndexMixin()
  107. {
  108.     IndexMixin = NewClass(sizeof(indexMixin_i), sizeof(indexMixin_c),
  109.                           "IndexMixin", END);
  110.     AddMethods(IndexMixin,
  111.         newGeneric,             _new,
  112.         END);
  113.         
  114.     AddClassMethods(IndexMixin,
  115.         allInstancesGeneric,     _allInstances,
  116.         deepInstancesGeneric,     _deepInstances,
  117.         sequenceGeneric,        _sequence,
  118.         END);
  119. }
  120.  
  121.